package com.cskaoyan.controller.backgroundsystemcontroller;


import com.cskaoyan.bean.bo.GrouponListBo;
import com.cskaoyan.bean.bo.GrouponListRecordBo;
import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.GoodsDetailVo;
import com.cskaoyan.bean.vo.GrouponListRecordVo;
import com.cskaoyan.bean.vo.GrouponListVo;
import com.cskaoyan.service.GoodsService;
import com.cskaoyan.service.PromotionService;
import com.cskaoyan.utils.InvalidatedParamsUtil;
import com.cskaoyan.utils.MyUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: lsy
 * @class: GrouponController2
 */
@RestController
@RequestMapping("/admin/groupon")
public class GrouponController {

    @Autowired
    PromotionService promotionService;

    @Autowired
    GoodsService goodsService;

    /**
     * @author: lsy
     * @title: list
     * @description: 查询团购规则
     * @param: [grouponListBo]
     * @return: com.cskaoyan.bean.vo.WxBaseRespVo
     */
    @GetMapping("list")
    @RequiresPermissions(value = {"*","admin:groupon:list"},logical = Logical.OR)
    public BaseRespVo list(GrouponListBo grouponListBo) {
        GrouponListVo grouponListVo = promotionService.queryGrouponRules(grouponListBo);
        return BaseRespVo.ok(grouponListVo);
    }

    @PostMapping("update")
    @RequiresPermissions(value = {"*","admin:groupon:update"},logical = Logical.OR)
    public BaseRespVo update(@RequestBody @Validated GrouponRules grouponRules, BindingResult result) {
        if (result.hasErrors()) {
            InvalidatedParamsUtil.resErrorVo(result);
        }
        GoodsDetailVo goodsDetailVo = goodsService.queryGoodsDetail(grouponRules.getGoodsId());
        Goods goods = goodsDetailVo.getGoods();
        grouponRules.setGoodsName(goods.getName());
        grouponRules.setDeleted(false);
        grouponRules.setPicUrl(goods.getPicUrl());

        promotionService.changeGrouponRules(grouponRules);
        return BaseRespVo.ok(grouponRules);
    }

    @PostMapping("delete")
    @RequiresPermissions(value = {"*","admin:groupon:delete"},logical = Logical.OR)
    public BaseRespVo delete(@RequestBody GrouponRules grouponRules) {
        GrouponRules grouponRules1 = new GrouponRules();
        grouponRules1.setId(grouponRules.getId());
        grouponRules1.setDeleted(true);
        promotionService.changeGrouponRules(grouponRules);
        return BaseRespVo.ok();
    }

    @PostMapping("create")
    @RequiresPermissions(value = {"*","admin:groupon:create"},logical = Logical.OR)
    public BaseRespVo create(@RequestBody @Validated GrouponRules grouponRules, BindingResult result) {
        if (result.hasErrors()) {
            return InvalidatedParamsUtil.resErrorVo(result);
        }
        GoodsDetailVo goodsDetailVo = goodsService.queryGoodsDetail(grouponRules.getGoodsId());
        Goods goods = goodsDetailVo.getGoods();
        grouponRules.setGoodsName(goods.getName());
        Date date = new Date();
        grouponRules.setAddTime(date);
        grouponRules.setDeleted(false);
        grouponRules.setPicUrl(goods.getPicUrl());
        grouponRules.setUpdateTime(date);

        promotionService.addGrouponRules(grouponRules);
        return BaseRespVo.ok(grouponRules);
    }

    /**
     * @author: lsy
     * @title: listRecord
     * @description: 查询团购活动
     * @param: [grouponListRecordBo]
     * @return: com.cskaoyan.bean.vo.WxBaseRespVo
     */
    @RequestMapping("listRecord")
    @RequiresPermissions(value = {"*","admin:groupon:listRecord"},logical = Logical.OR)
    public BaseRespVo listRecord(GrouponListRecordBo grouponListRecordBo) {
        List<OrderGoods> orderGoodsList = goodsService.queryOrdersByGoodsId(grouponListRecordBo.getGoodsId() == null ? 0 : grouponListRecordBo.getGoodsId());
        ArrayList<Integer> orderIdList = new ArrayList<>();
        for (OrderGoods orderGoods : orderGoodsList) {
            orderIdList.add(orderGoods.getOrderId());
        }
        GrouponListRecordVo grouponListRecordVo  = promotionService.queryGroupon(grouponListRecordBo, orderIdList);

        return BaseRespVo.ok(grouponListRecordVo);
    }
}